﻿using System.Linq;
using System.Windows.Forms;
using Microsoft.Office.PowerPoint.STAMP.Core;
using Microsoft.Office.PowerPoint.STAMP.Model;
using Microsoft.Office.PowerPoint.STAMP.Properties;

namespace Microsoft.Office.PowerPoint.STAMP.Command
{
    public class ImportCommand : ICommand
    {
        public void Execute()
        {
            using (var importForm = new AddCaptionsImport())
            {
                DialogResult dialogResult = importForm.ShowDialog();

                if (dialogResult == DialogResult.OK)
                {
                    if (importForm.EditRequired)
                    {
                        new EditorCommand(importForm.CaptionsList).Execute();
                    }
                    else
                    {
                        CaptionManager captionManager = new CaptionManager(importForm.Slide, importForm.Shape);

                        captionManager.SetCaptionsListMediaShapeName(importForm.Shape.Name, importForm.CaptionsList);

                        // Maps all the grid captions to internal object for rendering
                        CaptionItemsList captionList = Mapper.ToCaptions(importForm.CaptionsList.ClosedCaptions);

                        var timesGreaterMediaLength = captionList.CaptionItems.Any(a => (a.BookMarkEntryPosition > importForm.Shape.MediaFormat.Length ||
                            a.BookMarkExitPosition > importForm.Shape.MediaFormat.Length));

                        if (!captionManager.Validate(captionList) || timesGreaterMediaLength)
                        {
                            new EditorCommand(importForm.CaptionsList, captionManager.ValidationErrors).Execute();
                        }
                        else
                        {
                            int captionCount = captionList.CaptionItems.Count;
                            if (captionCount > Settings.Default.ProcessingNotificationLimit)
                            {
                                using (var formProgress = new Progress(captionCount))
                                {
                                    formProgress.Show(importForm);

                                    captionManager.RenderSingleSetup(captionList);

                                    for (int i = 0; i < captionCount; i++)
                                    {
                                        captionManager.RenderSingleCaptionItem(captionList.CaptionItems[i]);
                                        if (i%10 == 0)
                                        {
                                            formProgress.UpdateCreating(i);
                                        }
                                    }
                                    captionManager.SetDisplayAreaAndVisibilitySettings();
                                }
                            }
                            else
                            {
                                captionManager.RenderClosedCaptions(captionList);
                            }
                            captionManager.SaveCaptions();
                        }
                    }
                }
            }
        }
    }
}